package com.mazaiting.face.ui.widget;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import com.mazaiting.face.ui.R;
import com.mazaiting.face.ui.utils.DensityUtil;

/**
 * 人脸检测区域View
 */
public class FaceDetectRoundView extends View {
    /**
     * Surface 高度
     */
    public static final float SURFACE_HEIGHT = 1000f;
    /**
     * Surface 比例
     */
    public static final float SURFACE_RATIO = 0.75f;
    /**
     * 空格宽度比例
     */
    public static final float WIDTH_SPACE_RATIO = 0.33f;
    /**
     * 高度比例
     */
    public static final float HEIGHT_RATIO = 0.1f;
    /**
     * 高度比例
     */
    public static final float HEIGHT_EXT_RATIO = 0.2f;
    /**
     * 圆形间距
     */
    public static final int CIRCLE_SPACE = 5;
    /**
     * 路径间距
     */
    public static final int PATH_SPACE = 16;
    /**
     * 路径小间距
     */
    public static final int PATH_SMALL_SPACE = 12;
    /**
     * 路径宽度
     */
    public static final int PATH_WIDTH = 4;
    /**
     * 路径效果
     */
    private PathEffect mFaceRoundPathEffect;
    // new DashPathEffect(new float[]{PATH_SPACE, PATH_SPACE}, 1);
    /**
     * 背景画笔
     */
    private Paint mBgPaint;
    /**
     * 路径画笔
     */
    private Paint mPathPaint;
    /**
     * 脸部矩形画笔
     */
    private Paint mFaceRectPaint;
    /**
     * 脸部圆环画笔
     */
    private Paint mFaceRoundPaint;
    /**
     * 人脸矩形
     */
    private Rect mFaceRect;
    /**
     * 人脸检测矩形
     */
    private Rect mFaceDetectRect;
    /**
     * 圆心横坐标
     */
    private float mX;
    /**
     * 圆心纵坐标
     */
    private float mY;
    /**
     * 圆心半径
     */
    private float mR;
    /**
     * 路径宽度
     */
    private float mPathWidth;
    /**
     * 是否画虚线
     */
    private boolean mIsDrawDash = true;

    public FaceDetectRoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 将内容绘制到 Bitmap 中
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        // 获取路径间隙
        float pathSpace = DensityUtil.dip2px(context, PATH_SPACE);
        // 获取路径小间隙
        float pathSmallSpace = DensityUtil.dip2px(context, PATH_SMALL_SPACE);
        // 获取路径宽度
        mPathWidth = DensityUtil.dip2px(context, PATH_WIDTH);
        // 圆形路径效果
        mFaceRoundPathEffect = new DashPathEffect(
                new float[]{
                        pathSpace,
                        getResources().getDisplayMetrics().heightPixels < SURFACE_HEIGHT
                            ? pathSmallSpace : pathSpace},1);
        // 初始化背景画笔
        initBgPaint();
        // 初始化路径画笔
        initPathPaint();
        // 初始人脸矩形画笔
        initFaceRectPaint();
        // 初始化人脸圆形画笔
        initFaceRoundPaint();
    }

    /**
     * 初始化人脸圆形画笔
     */
    private void initFaceRoundPaint() {
        mFaceRoundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFaceRoundPaint.setColor(getResources().getColor(R.color.round_rect));
        mFaceRoundPaint.setStyle(Paint.Style.FILL);
        mFaceRoundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mFaceRoundPaint.setAntiAlias(true);
        mFaceRoundPaint.setDither(true);
    }

    /**
     * 初始化人脸矩形画笔
     */
    private void initFaceRectPaint() {
        mFaceRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFaceRectPaint.setColor(getResources().getColor(android.R.color.white));
        mFaceRectPaint.setStrokeWidth(mPathWidth);
        mFaceRectPaint.setStyle(Paint.Style.STROKE);
        mFaceRectPaint.setAntiAlias(true);
        mFaceRectPaint.setDither(true);
    }

    /**
     * 初始化路径画笔
     */
    private void initPathPaint() {
        mPathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPathPaint.setColor(getResources().getColor(R.color.round_rect));
        mPathPaint.setStrokeWidth(mPathWidth);
        mPathPaint.setStyle(Paint.Style.STROKE);
        mPathPaint.setAntiAlias(true);
        mPathPaint.setDither(true);
    }

    /**
     * 初始化背景画笔
     */
    private void initBgPaint() {
        // 创建画笔
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // 设置颜色
        mBgPaint.setColor(getResources().getColor(R.color.round_bg));
        // 设置风格
        mBgPaint.setStyle(Paint.Style.FILL);
        // 设置抗锯齿
        mBgPaint.setAntiAlias(true);
        // 防抖
        mBgPaint.setDither(true);
    }

    /**
     * 修改虚线状态
     * @param isDrawDash 是否绘制虚线
     */
    public void processDrawState(boolean isDrawDash) {
        mIsDrawDash = isDrawDash;
        // 强制重绘
        postInvalidate();
    }

    /**
     * 获取半径
     * @return 半径
     */
    public float getRound() {
        return mR;
    }

    /**
     * 获取人脸圆形矩形
     * @return 矩形
     */
    public Rect getFaceRoundRect() {
        return mFaceRect;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        // 计算宽度
        float canvasWidth = right - left;
        // 计算高度
        float canvasHeight = bottom - top;
        // 计算圆心横坐标
        float x = canvasWidth / 2;
        // 计算圆心纵坐标
        float y = (canvasHeight / 2) - ((canvasHeight / 2) * HEIGHT_RATIO);
        // 计算圆半径
        float r = (canvasWidth / 2) - ((canvasWidth / 2) * WIDTH_SPACE_RATIO);
        // 初始化人脸矩形
        if (mFaceRect == null) {
            mFaceRect = new Rect((int) (x - r),
                    (int) (y - r),
                    (int) (x + r),
                    (int) (y + r));
        }
        // 初始化人脸检测矩形
        if (mFaceDetectRect == null) {
            float hr = r + (r * HEIGHT_EXT_RATIO);
            mFaceDetectRect = new Rect((int) (x - r),
                    (int) (y - hr),
                    (int) (x + r),
                    (int) (y + hr));
        }
        // 赋值
        mX = x;
        mY = y;
        mR = r;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 设置透明度
        canvas.drawColor(Color.TRANSPARENT);
        // 设置画笔
        canvas.drawPaint(mBgPaint);
        // 判断是否设置路径效果
        if (mIsDrawDash) {
            mPathPaint.setPathEffect(mFaceRoundPathEffect);
        } else {
            mPathPaint.setPathEffect(null);
        }
        // 绘制大圆与小圆
        canvas.drawCircle(mX, mY, mR + CIRCLE_SPACE, mPathPaint);
        canvas.drawCircle(mX, mY, mR, mFaceRoundPaint);
    }

    /**
     * 获取预览检测矩形
     * @param w 显示宽度
     * @param pw 预览宽度
     * @param ph 预览高度
     * @return 矩形
     */
    public static Rect getPreviewDetectRect(int w, int pw, int ph) {
        double round = (w * 1.0 / 2) - ((w * 1.0 / 2) * WIDTH_SPACE_RATIO);
        double x = pw * 1.0 / 2;
        double y = (ph * 1.0 / 2) - ((ph * 1.0 / 2) * HEIGHT_RATIO);
        double r = (pw * 1.0 / 2) > round ? round : (pw * 1.0 / 2);
        double hr = r + (r * HEIGHT_EXT_RATIO);
        return new Rect((int)(x - r), (int) (y - hr), (int) (x + r), (int) (y + hr));
    }

}